package com.chen.facemanager.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chen.facemanager.bean.DriverLicense;
import com.chen.facemanager.bean.Local;
import com.chen.facemanager.bean.VehicleLicense;
import com.chen.facemanager.dao.DriverLicenseDao;
import com.chen.facemanager.dao.VehicleLicenseDao;
import com.chen.facemanager.dto.JSZ;
import com.chen.facemanager.dto.JszEntity;
import com.chen.facemanager.dto.People;
import com.chen.facemanager.dto.XSZ;
import com.chen.facemanager.util.HttpClient;
import com.chen.facemanager.util.Result;
import com.chen.facemanager.util.SSLClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.pl.PESEL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName OtherService
 * @Description
 * @Author Mr-CHEN
 * @Date 2019/8/22 14:20
 * @Version V1.0
 **/

@Api(value = "/service", tags = "第三方服务模块")
@RestController
@RequestMapping("/service")
public class OtherService {

    private final String JSZAPI = "https://182.50.124.210:9121/service/900103";

    private final String XSZAPI = "https://182.50.124.210:9121/service/900104";

    private HttpClient httpClient = new HttpClient();

    @Autowired
    private VehicleLicenseDao vehicleLicenseDao;
    @Autowired
    private DriverLicenseDao driverLicenseDao;

    @ApiOperation(value = "驾驶证", notes = "驾驶证")
    @PostMapping("/jsz")
    public JSZ getGsz(@RequestBody JszEntity jszEntity) {
        DriverLicense driverLicense = new DriverLicense(Long.parseLong(jszEntity.getUid()), "2027-09-29 00:00:00",
                "2011-09-29 00:00:00",
                "A/正常",
                "2017-09-29 00:00:00",
                "1/男性",
                "C1/小型汽车",
                "");
        driverLicenseDao.save(driverLicense);
        return new JSZ();
//        Map<String, String> parms = new HashMap<>();
//        parms.put("name", name);
//        parms.put("idnumber", idnumber.toUpperCase());
//        parms.put("terminalid", terminalid);
//        String result = httpClient.doPost(JSZAPI, parms, "UTF-8");
//
//        JSONObject userJson = JSONObject.parseObject(result);
//        JSZ jsz = JSON.toJavaObject(userJson, JSZ.class);
//
//        if (1 == jsz.getResult()) {
//            DriverLicense driverLicense = new DriverLicense(Long.parseLong(uid), jsz.getResultData().getValidTo(),
//                    jsz.getResultData().getFirstIssueDate(),
//                    jsz.getResultData().getLicenseStatus(),
//                    jsz.getResultData().getValidFrom(),
//                    jsz.getResultData().getGender(),
//                    jsz.getResultData().getLicenseClass(),
//                    jsz.getResultData().getLicenseType());
//            driverLicenseDao.save(driverLicense);
//        } else {
//            DriverLicense driverLicense = new DriverLicense(Long.parseLong(uid), "2027-09-29 00:00:00",
//                    "2011-09-29 00:00:00",
//                    "A/正常",
//                    "2017-09-29 00:00:00",
//                    "1/男性",
//                    "C1/小型汽车",
//                    "");
//            driverLicenseDao.save(driverLicense);
//        }

//        return jsz;
    }

    @ApiOperation(value = "行驶证", notes = "行驶证")
    @PostMapping("/xsz")
    public XSZ getXsz(String name, String idnumber, String licensePlateNumber, String terminalid, String uid) {
        Map<String, String> parms = new HashMap<>();
        parms.put("name", name);
        parms.put("idnumber", idnumber.toUpperCase());
        parms.put("licensePlateNumber", licensePlateNumber);
        parms.put("terminalid", terminalid);
        String result = httpClient.doPost(XSZAPI, parms, "UTF-8");

        JSONObject userJson = JSONObject.parseObject(result);
        XSZ xsz = JSON.toJavaObject(userJson, XSZ.class);

        if (1 == xsz.getResult()) {
            VehicleLicense vehicleLicense = new VehicleLicense(Long.parseLong(uid),
                    xsz.getResultData().getFirstRegisterDate(),
                    xsz.getResultData().getLicensePlateType(),
                    xsz.getResultData().getVehicleStatus()
            );
            vehicleLicenseDao.save(vehicleLicense);
        }
        return xsz;
    }


    @ApiOperation(value = "获取驾驶证", notes = "获取驾驶证")
    @PostMapping("/jsz/one")
    public Result getDriverLicense(String uid) {
        DriverLicense license = driverLicenseDao.findDriverLicenseByUid(Long.parseLong(uid));
        if (license != null) {
            return new Result(200, "ok", license);
        } else {
            return new Result(400, "error", null);
        }
    }

    @ApiOperation(value = "获取行驶证", notes = "获取行驶证")
    @PostMapping("/xsz/one")
    public Result getVehicleLicense(String uid) {
        VehicleLicense license = vehicleLicenseDao.findVehicleLicenseByUid(Long.parseLong(uid));
        if (license != null) {
            return new Result(200, "ok", license);
        } else {
            return new Result(400, "error", null);
        }
    }

    @GetMapping("/data1.json")
    public Result data() {
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> res1 = new HashMap<>();
        res1.put("type", "1");
        res1.put("monery", "1");

        Map<String, String> res2 = new HashMap<>();
        res2.put("type", "2");
        res2.put("monery", "0");
        list.add(res1);
        list.add(res2);
        return new Result(200, "ok", list);
    }
}